package com.xgp.labblogaffter.Controller;

import com.xgp.labblogaffter.mapper.SpeakMapper;
import com.xgp.labblogaffter.mapper.SpeakMapperExt;
import com.xgp.labblogaffter.model.Speak;
import com.xgp.labblogaffter.model.SpeakExample;
import com.xgp.labblogaffter.model.WritesExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SpeakController {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    SpeakMapper speakMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    SpeakMapperExt speakMapperExt;
    /**
     * 根据id删除评论
     * @param sid
     * @return
     */
    @PostMapping("/deleteSpeak")
    public boolean deleteSpeak(String sid) {
        try {
            int flag = speakMapper.deleteByPrimaryKey(Integer.parseInt(sid));
            if(flag == 1) {
                return true;
            }
            return false;
        }catch (Exception e) {
            return false;
        }

    }

    @GetMapping("/findSpeaksAllByTime")
    public List<Speak> findSpeaksAllByTime() {
        try {
            SpeakExample writesExample = new SpeakExample();
            writesExample.setOrderByClause("time desc");

            return speakMapper.selectByExample(writesExample);
        }catch (Exception e) {
            return null;
        }


    }

    @GetMapping("/findSpeaksAll")
    public List<Speak> findSpeaksAll() {
        try {
            return speakMapperExt.findSpeaksAll();
        }catch (Exception e) {
            return null;
        }
    }

    /**
     * 放回评论数量
     * @return
     */
    @GetMapping("/SpeakCount")
    public long SpeakCount() {
        try {
            return speakMapperExt.SpeakCount();
        }catch (Exception e) {
            return 0;
        }
    }

}
